﻿using System.Collections.Generic;
using System;
using System.Text;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.ComponentModel;

namespace commonV2
{
    public class TimerProcess
    {
        public TimerProcess()
        {
            bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
        }
        System.ComponentModel.BackgroundWorker bgWorker = new System.ComponentModel.BackgroundWorker();
        DateTime lastRunnTime = DateTime.Now;
        private short _waitSeconds = 0;
        public short WaitInSeconds
        {
            get { return _waitSeconds;}
            set 
            { 
                _waitSeconds = value;
                lastRunnTime = DateTime.Now;
            }
        }

        public delegate void OnProcessEvent();
        public event OnProcessEvent OnProcess = null;

        private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (OnProcess == null) return;
            OnProcess();
        }
        public bool IsEndWaitTime()
        {
            //Set WaitSeconds to 0 to disable the process
            if (WaitInSeconds <= 0 ||
                dateTimeLibs.DateDiffInSecs(lastRunnTime, DateTime.Now) < WaitInSeconds)
                return false;
            return true;
        }
        public bool Execute()
        {
            if (!IsEndWaitTime()) return false;
            if (bgWorker.IsBusy) return false;
            bgWorker.RunWorkerAsync();
            lastRunnTime = DateTime.Now;
            return true;
        }

        private Thread myThread = null;
        private bool _Execute(bool nonStop)
        {
            if (OnProcess == null) return false;
            //Set WaitSeconds to 0 to disable the process
            //int test = dateTimeLibs.DateDiffInSecs(lastRunnTime, DateTime.Now);
            if (WaitInSeconds <= 0 ||
                dateTimeLibs.DateDiffInSecs(lastRunnTime, DateTime.Now) < WaitInSeconds) 
                return false;
            //SysLog.WriteLog(" - Start " + lastRunnTime.ToString() + " " + DateTime.Now.ToString()+ " " + test.ToString());

            if (myThread == null)
            {
                myThread = new Thread(delegate()
                {
                    OnProcess();
                });
            }
            else
            {
                try
                {
                    //Thread.Join(TimeSpan.Zero) It will not block the caller and returns a value indicating whether the thread has completed its work
                    if (myThread.Join(TimeSpan.Zero))
                    {
                        myThread = null;
                        return true;
                    }
                    else return false;
                }
                catch { }
            }
            //if (myThread.IsAlive) return false;
            myThread.Start();
            if (nonStop)
                myThread.Join();
            lastRunnTime = DateTime.Now;
            return true; 
        }
    }
}